package com.my.zl.console.service.autocode.impl;

import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.my.zl.common.dto.ReqDTO;
import com.my.zl.common.dto.advancedQuery.PageQueryDTO;
import com.my.zl.common.enums.resp.GlobalEnum;
import com.my.zl.common.exception.BizException;
import com.my.zl.common.utils.CopyUtil;
import com.my.zl.console.common.constant.AutocodeC;
import com.my.zl.console.common.constant.AutocodeKeyC;
import com.my.zl.console.dao.autocode.ZlTableSequenceMapper;
import com.my.zl.console.entity.autocode.ZlTableSequence;
import com.my.zl.console.po.autocode.tableSequence.ZlTableSequenceVo;
import com.my.zl.console.dto.autocode.tableSequence.ReqTableSequenceAddDTO;
import com.my.zl.console.dto.autocode.tableSequence.ReqTableSequenceDelDTO;
import com.my.zl.console.dto.autocode.tableSequence.ReqTableSequenceEditDTO;
import com.my.zl.console.dto.autocode.tableSequence.ReqTableSequenceQueryDTO;
import com.my.zl.console.dto.autocode.tableSequence.ReqTableSequenceDetailDTO;
import com.my.zl.console.dto.autocode.tableSequence.ReqTableSequenceStateDTO;
import com.my.zl.console.dto.autocode.tableSequence.ResTableSequenceAddDTO;
import com.my.zl.console.dto.autocode.tableSequence.ResTableSequenceDelDTO;
import com.my.zl.console.dto.autocode.tableSequence.ResTableSequenceEditDTO;
import com.my.zl.console.dto.autocode.tableSequence.ResTableSequenceQueryDTO;
import com.my.zl.console.dto.autocode.tableSequence.ResTableSequenceDetailDTO;
import com.my.zl.console.dto.autocode.tableSequence.ResTableSequenceStateDTO;
import com.my.zl.console.dto.autocode.tableSequence.ResTableSequenceVO;
import com.my.zl.console.dto.autocode.tableSequence.ResTableSequenceVO2;
import com.my.zl.console.service.autocode.ITableSequenceService;
import com.my.zl.console.service.base.BaseManager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;

/**
 * 数据库表自增序号
 * @author ZL
 */
@Service
public class TableSequenceServiceImpl extends BaseManager implements ITableSequenceService {

    @Autowired
    private ZlTableSequenceMapper tableSequenceMapper;

    @Override
    public ResTableSequenceQueryDTO queryTableSequenceList(ReqTableSequenceQueryDTO reqInfo,PageQueryDTO page) throws Exception{
        // TODO Auto-generated method stub
        ResTableSequenceQueryDTO resDTO = BeanUtil.copyProperties(page, ResTableSequenceQueryDTO.class);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqInfo);
        
        pageHandle(page);//处理分页数据
        //当效率不行时自行计数，或完全自行分页，先查询总数，如果为0不用再次查询了；开启计数：true是,false否
        //临近第一条SQL查询语句自动分页,如果未消费时(即后面没有触发SQL查询)则需主动调用pageHp.clear()清理
        Page<Object> pageHp = PageHelper.startPage(page.getPage(), page.getLimit(),page.getSortorder());
        List<ZlTableSequenceVo> list = tableSequenceMapper.selectListPage(paramMap);
        resDTO.setData(CopyUtil.copyList(list, ResTableSequenceVO2.class));
        resDTO.setCount(pageHp.getTotal());
        
        return resDTO;
    }
    
    @Override
    public ResTableSequenceDetailDTO queryTableSequenceDetail(ReqTableSequenceDetailDTO reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ResTableSequenceDetailDTO res = new ResTableSequenceDetailDTO();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqInfo);
        ZlTableSequence view = tableSequenceMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(view, res);
        return res;
    }
    
    @Override
    public ResTableSequenceAddDTO insertTableSequenceInfo(ReqDTO<ReqTableSequenceAddDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqTableSequenceAddDTO reqData = reqInfo.getReqData();
        ResTableSequenceAddDTO res = new ResTableSequenceAddDTO();
        Instant time = getSystemTime().getUtcTime();
        
        ZlTableSequence record = new ZlTableSequence();
        BeanUtil.copyProperties(reqData, record);
        
        initCreateInfo(record, reqInfo, time);
        record.setSequenceId(IdUtil.simpleUUID());
        record.setDataStatus(AutocodeC.DataStatus.ENABLE.getCode());//状态
        
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("sequenceId",record.getSequenceId());
        //检查主键是否冲突
        if(tableSequenceMapper.countExistKey(paramMap)>0){
            throw new BizException(GlobalEnum.DUPLICATE_PRIMARY_KEY);
        }
        
        tableSequenceMapper.insertSelective(record);
        
        ZlTableSequence keyInfo = tableSequenceMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResTableSequenceEditDTO updateTableSequenceInfo(ReqDTO<ReqTableSequenceEditDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqTableSequenceEditDTO reqData = reqInfo.getReqData();
        ResTableSequenceEditDTO res = new ResTableSequenceEditDTO();
        Instant time = getSystemTime().getUtcTime();
        
        ZlTableSequence record = new ZlTableSequence();
        BeanUtil.copyProperties(reqData, record);
        
        initUpdateInfo(record, reqInfo, time);
        
        tableSequenceMapper.updateByPrimaryKeySelective(record);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("sequenceId",record.getSequenceId());
        ZlTableSequence keyInfo = tableSequenceMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResTableSequenceDelDTO deleteTableSequenceInfo(ReqDTO<ReqTableSequenceDelDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqTableSequenceDelDTO reqData = reqInfo.getReqData();
        ResTableSequenceDelDTO res = new ResTableSequenceDelDTO();
        Instant time = getSystemTime().getUtcTime();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqData);
        tableSequenceMapper.deleteByMap(paramMap);
        
        return res;
    }
    
    @Override
    public ResTableSequenceStateDTO updateTableSequenceState(ReqDTO<ReqTableSequenceStateDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqTableSequenceStateDTO reqData = reqInfo.getReqData();
        ResTableSequenceStateDTO res = new ResTableSequenceStateDTO();
        Instant time = getSystemTime().getUtcTime();
         
        ZlTableSequence record = new ZlTableSequence();
        BeanUtil.copyProperties(reqData, record);
        initUpdateInfo(record, reqInfo, time);
        tableSequenceMapper.updateByPrimaryKeySelective(record);
         
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("sequenceId",record.getSequenceId());
        ZlTableSequence keyInfo = tableSequenceMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
}